package org.bugjail.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Iterator;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.bugjail.data.Person;
import org.bugjail.data.Project;
import org.bugjail.servlet.language.LanguageStringLoader;

/**
 * Servlet implementation class for Servlet: HomeServlet
 *
 */
public class HomeServlet extends BasicUIServlet {
	
	static final long serialVersionUID = 1L;
	
	private final int MAX_PROJECTS_ON_HOME = 10;

	@Override
	protected void write(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		
		Person user = ServletUtilities.getLogedUser(request);
		
		PrintWriter pw = response.getWriter();
		
		pw.print("<br><br><br>");
		/*pw.print("<table width='100%'><tr>");
		pw.print("<td width='50%' valign=top><table class='hometable'><tr><th>My projects</th></tr>");
		if(user.getProjects() != null && user.getProjects().size() > 0){

			Iterator<Project> iterator = user.getProjects().keySet().iterator();
			int count = 0;
			while(iterator.hasNext() && count<MAX_PROJECTS_ON_HOME){
				Project project = iterator.next();
				count++;
				pw.print("<tr><td><a href='SelectProjectServlet?project="+project.getId()+"'>" + retrieveAllowedStringFromData(project.getName()) + "</a></td></tr>");
			}
		} else {
			pw.print("<tr><td>There is no Project associated to this User.</td></tr>");
		}
		pw.print("</table>");
		if (user.getProjects() != null && user.getProjects().size() > MAX_PROJECTS_ON_HOME) {
			pw.print("<p align=right><a href='ProjectServlet'>view more</a></p>");
		}
		pw.print("</td>");
		pw.print("<td width='50%' valign=top><table class='hometable'><tr><th>Last executions</th></tr><tr><td>");
		pw.print("-");
		pw.print("</td></tr></table></td>");
		pw.print("</tr><tr>");
		pw.print("<td width='50%' valign=top><table class='hometable'><tr><th>Last created test cases</th></tr><tr><td>");
		pw.print("-");
		pw.print("</td></tr></table></td>");
		pw.print("<td width='50%' valign=top><table class='hometable'><tr><th>Last versioned documents</th></tr><tr><td>");
		pw.print("-");
		pw.print("</td></tr></table></td>");
		pw.print("</tr></table>");
		pw.print("You are logged in as: " + retrieveAllowedStringFromData(getLoggedUser().getName()));*/
	}

	@Override
	protected String pageTitle() {
		return LanguageStringLoader.loadString(LanguageStringLoader.STR_HOME, getLoggedUser().getLanguage(), "Home");
	}

	@Override
	protected void beforeAction(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}   	  	    
}